package inc.zhugastrov.sellerform;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import static inc.zhugastrov.dbactions.DBActions.cancelOrder;
import static inc.zhugastrov.dbactions.DBActions.updateDBFromBasket;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class CurrentOrdersPane extends JPanel implements ActionListener {
	private static CurrentOrdersPane instance = new CurrentOrdersPane();
	private JTable catalog;
	private JTable items;
	private OrdersTableModel model = new OrdersTableModel();
	private ItemsTableModel itemsModel = new ItemsTableModel();
	private JScrollPane scrollPane;
	private JScrollPane scrollItemsPane;
	private JButton cancelButton;

	private CurrentOrdersPane() {
		init();
	}

	public static CurrentOrdersPane getInstance() {
		return instance;
	}

	public void addOrder(OrderIntr order) {
		model.addRow(order);
	}

	private void init() {
		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(this);
		add(cancelButton);

		catalog = new JTable();
		catalog.setModel(model);
		catalog.getTableHeader().setReorderingAllowed(false);
		catalog.setFocusable(false);
		catalog.setSelectionMode(0);
		catalog.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {

				itemsModel.emptyTable();
				List<OrderedItems> products = model.getRow(
						catalog.getSelectedRow()).getItems();
				for (Product item : products) {
					itemsModel.addRow(item);
					items.updateUI();
				}
			}

		});
		scrollPane = new JScrollPane(catalog);
		catalog.setPreferredScrollableViewportSize(new java.awt.Dimension(500,
				300));
		add(scrollPane);

		items = new JTable();
		items.setModel(itemsModel);
		items.getTableHeader().setReorderingAllowed(false);
		items.setFocusable(false);
		items.setSelectionMode(0);
		scrollItemsPane = new JScrollPane(items);
		items.setPreferredScrollableViewportSize(new java.awt.Dimension(500,
				150));
		add(scrollItemsPane);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(catalog.getSelectedRow() == -1){
			JOptionPane.showMessageDialog(this,
					"No orders selected, don't you see this?", "Error",
					JOptionPane.ERROR_MESSAGE);
			
			return;
		}
		int n = JOptionPane.showConfirmDialog(this, "Really whant to do this?",
				"Cancel order?", JOptionPane.YES_NO_OPTION);
		if (n == JOptionPane.YES_OPTION) {
			MainPanel mp = MainPanel.getInstance();
			int selectedRow = catalog.getSelectedRow();
			catalog.updateUI();
			Order order =(Order) model.getRow(selectedRow);
			for (Product item : order.getItems()) {
				Item itemToChange = (Item) mp.getModel().findWithId(
						item.getId());
				itemToChange.setAmountInStore(itemToChange.getAmountInStore()
						+ item.getAmountInStore());
			}
			updateDBFromBasket(order.getItems());
			cancelOrder(order.getId());
			model.deleteRow(selectedRow);
			itemsModel.emptyTable();
		}
	}
}
